package com.wtsw.pulsosocial.view.event;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.MarkerOptions;
import com.wtsw.pulsosocial.R;
import com.wtsw.pulsosocial.controller.GestorEventos;
import com.wtsw.pulsosocial.model.structure.Evento;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowEvento extends FragmentActivity implements TabListener{
	
	private GoogleMap mMap;
	private Evento evento;
	private TextView nombre;
	private TextView categoria;
	private TextView sub_categoria;
	private Button facebook_place;
	private ImageView categoria_img;
	private ImageView sub_categoria_img;
	
	public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       Intent myIntent= getIntent(); 
       String event_id = myIntent.getStringExtra("EventID");
       evento = GestorEventos.findEvent(Integer.valueOf(event_id));
       
       setContentView(R.layout.show_evento);
       
       nombre = (TextView) findViewById(R.id.nombre);
       categoria = (TextView) findViewById(R.id.categoria_txt);
       sub_categoria = (TextView) findViewById(R.id.sub_categoria_txt);
       facebook_place = (Button) findViewById(R.id.pulse_button);
       categoria_img = (ImageView) findViewById(R.id.categoria_img);
       sub_categoria_img = (ImageView) findViewById(R.id.sub_categoria_img);
       
       nombre.setText(evento.getNombre());
       categoria.setText(evento.getSubCategoria().getPadre().getNombre());
       sub_categoria.setText(evento.getSubCategoria().getNombre());
       if (evento.getLugar() != null) {
    	   facebook_place.setText(evento.getLugar().getName());
    	   facebook_place.setVisibility(Button.VISIBLE);
       }
       categoria_img.setImageBitmap(evento.getSubCategoria().getPadre().getIcono());
       sub_categoria_img.setImageBitmap(evento.getSubCategoria().getIcono());
       
       facebook_place.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {       		       		
       				try {		           				    
       				    getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0);
       				 	Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/"+evento.getLugar().getId()));
       				    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       				    getApplicationContext().startActivity(intent);       				       					
       				} catch (Exception e) {			   
       					Intent intent = new Intent();
       					intent.setAction(Intent.ACTION_VIEW);
       					intent.addCategory(Intent.CATEGORY_BROWSABLE);
       					intent.setData(Uri.parse("http://facebook.com/"+evento.getLugar().getId()));
       					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       					getApplicationContext().startActivity(intent);
       				}     
           }
       });
       
       ActionBar bar = getActionBar();
       bar.addTab(bar.newTab().setText("Evento").setTabListener(this));
       bar.addTab(bar.newTab().setText("El Chull").setTabListener(this));

       bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
       bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
       bar.setDisplayShowHomeEnabled(true);
       // remove the activity title to make space for tabs
       bar.setDisplayShowTitleEnabled(false);
       
       setUpMapIfNeeded();	   
    }
	
	private void setUpMapIfNeeded() {
		 // Do a null check to confirm that we have not already instantiated the map.
       if (mMap == null) {
           // Try to obtain the map from the SupportMapFragment.
           mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                   .getMap();
           // Check if we were successful in obtaining the map.            	            
           if (mMap != null)	
               setUpMap();
       }
	}

	private void setUpMap() {
       mMap.setMyLocationEnabled(true);          
       mMap.setMapType(1);       
       mMap.addMarker(new MarkerOptions()
       .position(evento.getUbicacion()).title(evento.getNombre())           
       .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));  
       CameraPosition cameraPosition = new CameraPosition.Builder()
       .target(evento.getUbicacion())      // Sets the center of the map to Mountain View
       .zoom(17)                   // Sets the zoom
       .bearing(0)                // Sets the orientation of the camera to east
       .tilt(45)                   // Sets the tilt of the camera to 30 degrees
       .build();                   // Creates a CameraPosition from the builder    
       mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));       
   }

	@Override
	public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

     
}
